﻿using System;
using System.Collections;
using System.Collections.Generic;
using StockHelper.Scripts.Types;

namespace StockHelper.Scripts.Stock
{

    public class StockCtrl
    {
        public static Dictionary<string, StockData> stockData = new Dictionary<string, StockData> { };


        public static void Init()
        {
            foreach (string k in Config.Get("stockList", new ArrayList()))
                stockData.Add(k, new StockData());
            _GetNewData(true);
        }


        public static void Add(string url) {
            url = url.Trim();
            if (!stockData.ContainsKey(url)) {
                stockData.Add(url, new StockData());
                _GetNewData(true);

                Config.Get("stockList", new ArrayList()).Add(url);
                Config.Save();
            }
        }

        public static void Remove(string url)
        {
            stockData.Remove(url);
            ArrayList list = new ArrayList();
            foreach (string k in stockData.Keys)
               if(k != url) list.Add(k);
            Config.Set("stockList", list);
        }
        

        public static StockData GetData(string url) {
            foreach (string i in stockData.Keys) {
                if (i == url)
                    return stockData[i];
            }
            return null;
        }

        public static object GetData(string key, Param param) {
            if (stockData.ContainsKey(key))
                return stockData[key].Get(param);
            else
             return "";
        }

        
        public static void _GetNewData(bool restart = false)
        {
            Sina.Updata(restart);
        }
        
        public static void GetNewData()
        {
            _GetNewData();
        }

        public static void Clear() {
            stockData.Clear();
        }




    }
}